package com.zcs.app.lucky.entity

import android.text.TextUtils
import cn.sxw.android.base.mvp.CheckableEntity
import cn.sxw.android.base.utils.JDateKit
import com.alibaba.fastjson.JSON
import java.io.Serializable
import java.util.*
import kotlin.collections.ArrayList

const val TYPE_USER_ADD = 0
const val TYPE_USER_NORMAL = 1
const val KEY_FOR_CHECK = "KEY_FOR_CHECK"

val AVATAR_LIST = listOf(
    "http://img.bq233.com/kanqq/pic/upload/2018/0723/1532329172809328.jpg",
    "https://up.enterdesk.com/edpic/84/69/a9/8469a992634242df4291b3265f9f2a38.jpg",
    "https://up.enterdesk.com/edpic_source/29/d0/11/29d011b17e6c23fbdb21b26ad7f6cbef.jpg",
    "https://up.enterdesk.com/edpic_source/e6/9d/1d/e69d1d605eb72b5bda16ed1eb9d03563.jpg",
    "https://up.enterdesk.com/edpic/dd/e7/6e/dde76ec25f22c62aaabc4d73902802d3.jpg",
    "https://up.enterdesk.com/edpic/4e/28/f2/4e28f2652b57293e046aff1d73d8c720.jpg",
    "https://up.enterdesk.com/edpic_source/89/18/15/891815cbbbf33c266210b8b43f2049d7.jpg",
    "https://up.enterdesk.com/edpic_source/98/d0/f3/98d0f3fe360cb8dafe3498f322287707.jpg",
    "https://up.enterdesk.com/edpic_source/97/58/54/9758540981541ee99a39b9fd61e2389e.jpg"
)

// 人员对象
class LuckyPeople : Serializable {
    constructor()

    constructor(type: Int) {
        this.type = type
        this.id = UUID.randomUUID().toString()
    }

    var id: String = ""

    // 成员名称
    var name: String? = null

    var type: Int = TYPE_USER_NORMAL

    // 序号
    var rank: Int = 0

    // 头像
    var avatar: String? = null

    // 是否已中奖
    var hasLucky: Boolean = false

    var dept: String? = null
    var phone: String? = null

    fun isAdd(): Boolean {
        return type == TYPE_USER_ADD
    }

    fun isEmpty(): Boolean = TextUtils.isEmpty(id)
}

// 分组对象
class GroupEntity {
    constructor() {
        this.createDate = System.currentTimeMillis()
        this.id = UUID.randomUUID().toString()
    }

    var id: String = ""
    var name: String = ""
    var list = ArrayList<LuckyPeople>()
    var desc: String = ""
    var createDate: Long = 0

    fun niceDesc(): String = if (TextUtils.isEmpty(desc)) "暂无说明" else desc

    fun niceName(): String {
        return "$name (${list.size}人)"
    }
}

// 奖品对象
class PrizeEntity : CheckableEntity {
    constructor() {
        this.id = UUID.randomUUID().toString()
    }

    var id: String = ""
    var name: String = ""
    var level: LevelEntity? = null
    var maxCount: Int = 0
    var pic: String = ""

    fun copy(): PrizeEntity {
        val json = JSON.toJSONString(this)
        return JSON.parseObject(json, PrizeEntity::class.java)
    }
}

const val LEVEL_0 = 0
const val LEVEL_1 = 1
const val LEVEL_2 = 2
const val LEVEL_3 = 3
const val LEVEL_4 = 4
const val LEVEL_5 = 5
const val LEVEL_6 = 6
const val LEVEL_7 = 7
const val LEVEL_8 = 8
const val LEVEL_9 = 9
const val LEVEL_X = 10

class LevelEntity : CheckableEntity {
    constructor()

    constructor(level: Int, levelName: String) {
        this.id = level
        this.name = levelName
    }

    // 默认幸运奖
    var id: Int = LEVEL_9
    var name: String = ""
}

fun levelList(): ArrayList<LevelEntity> {
    return arrayListOf(
        LevelEntity(LEVEL_0, "特等奖"),
        LevelEntity(LEVEL_1, "一等奖"),
        LevelEntity(LEVEL_2, "二等奖"),
        LevelEntity(LEVEL_3, "三等奖"),
        LevelEntity(LEVEL_4, "四等奖"),
        LevelEntity(LEVEL_5, "五等奖"),
        LevelEntity(LEVEL_6, "六等奖"),
        LevelEntity(LEVEL_7, "七等奖"),
        LevelEntity(LEVEL_8, "八等奖"),
        LevelEntity(LEVEL_9, "幸运奖"),
        LevelEntity(LEVEL_X, "未中奖"),
    )
}

// 抽奖对象
class LuckyEntity : CheckableEntity {
    constructor() {
        this.id = UUID.randomUUID().toString()
    }

    var id: String = ""
    var name: String = ""
    var desc: String = ""
    var group: GroupEntity? = null
    var prizeList = ArrayList<PrizeEntity>()
    var createTime: Long = 0

    // 抽奖结果
    var resultList = ArrayList<LuckyResultEntity>()

    fun niceName(): String {
        return "$name (${group?.list?.size}人)"
    }

    fun niceDesc(): String {
        return "抽奖时间：${niceTime()}\n抽奖主题：$desc"
    }

    fun niceTime(): String {
        return JDateKit.timeToDate(JDateKit.fmtYmdHms, createTime)
    }

    // 抽奖状态
    fun statusName(): String = if (canLucky()) "未抽奖" else "${luckyCount()}人中奖"

    private fun luckyCount(): Int {
        var count = 0
        for (result in resultList) {
            count += result.userList.size
        }
        return count
    }

    // 是否可以继续抽奖
    fun canLucky(): Boolean = resultList.isNullOrEmpty()
}

// 抽奖结果
class LuckyResultEntity {
    constructor()

    var prize: PrizeEntity? = null
    var userList = ArrayList<LuckyPeople>()
}
